package com.yl.auth.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yl.auth.model.entity.Fenjigou;
import com.yl.auth.model.vo.FenJiGouVo;
import com.yl.auth.service.FenjigouService;
import com.yl.common.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author L
 * @since 2023-07-21
 */
@RestController
@RequestMapping("/admin/yanglao/fenjigou")
@Api(tags = "机构中心端-分机构管理")
public class FenjigouController {
    @Resource
    private FenjigouService fenjigouService;

    @ApiOperation("全查+分页+条件查询")
    @GetMapping("/findAllConAndPage/{page}/{limit}")
    public Result findAllConAndPage(@PathVariable Integer page, @PathVariable Integer limit, FenJiGouVo fenJiGouVo){
        Page<Fenjigou> fenjigouPage = new Page<>(page,limit);
        LambdaQueryWrapper<Fenjigou> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        String institutionName = fenJiGouVo.getInstitutionName();
        String name = fenJiGouVo.getName();
        if (!StringUtils.isEmpty(institutionName)){
            lambdaQueryWrapper.like(Fenjigou::getInstitutionName,institutionName);
        }
        if (!StringUtils.isEmpty(name)){
            lambdaQueryWrapper.like(Fenjigou::getName,name);
        }
        lambdaQueryWrapper.orderByDesc(Fenjigou::getId);
        Page<Fenjigou> fenjigouPage1 = fenjigouService.page(fenjigouPage, lambdaQueryWrapper);
        return Result.ok(fenjigouPage1);
    }


    @ApiOperation("添加机构信息")
    @PostMapping("/save")
    public Result save(@RequestBody Fenjigou fenjigou){
        fenjigou.setCreateTime(LocalDateTime.now());
        fenjigouService.save(fenjigou);
        return Result.ok();
    }

    @ApiOperation("根据id查询指定机构信息")
    @GetMapping("/getById/{id}")
    public Result getById(@PathVariable Long id){
        Fenjigou fenjigou = fenjigouService.getById(id);
        return Result.ok(fenjigou);
    }

    @ApiOperation("修改机构信息")
    @PostMapping("/update")
    public Result update(@RequestBody Fenjigou fenjigou){
        fenjigouService.updateById(fenjigou);
        return Result.ok();
    }

    @ApiOperation("根据id删除指定机构信息")
    @DeleteMapping("/deleteById/{id}")
    public Result deleteById(@PathVariable Long id){
        fenjigouService.removeById(id);
        return Result.ok();
    }
}

